package com.business.activiti.controller;

import com.business.activiti.model.auto.TaskNode;
import com.business.activiti.service.TaskNodeService;
import com.business.system.common.base.BaseController;
import com.business.system.common.domain.AjaxResult;
import com.business.system.model.custom.TableSplitResult;
import com.business.system.model.custom.Tablepar;
import com.business.system.model.custom.TitleVo;
import com.business.system.shiro.util.ShiroUtils;
import com.business.util.ListUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("TaskNodeController")
@Api(value = "任务节点")
public class TaskNodeController extends BaseController{
	
	private String prefix = "gen/taskNode";
	@Autowired
	private TaskNodeService taskNodeService;


	@GetMapping("noHandleTaskView")
	public String noHandleTaskView(ModelMap model)
	{
		String str="待办任务";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
		return prefix + "/noHandleTaskList";
	}

	@GetMapping("handleTaskView")
	public String handleTaskView(ModelMap model)
	{
		String str="已办任务";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
		return prefix + "/handleTaskList";
	}

	@GetMapping("taskForm/{formId}/{taskId}")
	public String taskForm(@PathVariable("formId") String formId,@PathVariable("taskId") String taskId,ModelMap model) {
		model.put("formKey",formId);
		model.put("taskId",taskId);
		return prefix + "/taskForm";
	}
	@GetMapping("view")
    public String view(ModelMap model){
		String str="任务节点";
		setTitle(model, new TitleVo("列表", str+"管理", true,"欢迎进入"+str+"页面", true, false));
        return prefix + "/list";
    }
	
	@PostMapping("list")
	@ResponseBody
	public Object list(Tablepar tablepar,String searchTxt){
		PageInfo<TaskNode> page=taskNodeService.list(tablepar,searchTxt) ; 
		TableSplitResult<TaskNode> result=new TableSplitResult<TaskNode>(page.getPageNum(), page.getTotal(), page.getList()); 
		return  result;
	}

	/**
	 * 当前登录用户待办任务
	 * @param tablepar
	 * @return
	 */
	@RequestMapping("assignmentTasks")
	@ResponseBody
	public Object assignmentTasks(Tablepar tablepar){
		List<Map<String,String>> list=taskNodeService.getTaskList(ShiroUtils.getUser());
		List<Map<String,String>> listPage= (List<Map<String, String>>) ListUtils.getSubList(list,tablepar.getPageNum(),tablepar.getPageSize());
		TableSplitResult<Map<String,String>> result=new TableSplitResult<Map<String,String>>(tablepar.getPageNum(),Long.valueOf(list.size()), listPage);
		return  result;
	}

	/**
	 * 当前登录用户历史任务
	 * @param tablepar
	 * @return
	 */
	@RequestMapping("historyTasks")
	@ResponseBody
	public Object historyTasks(Tablepar tablepar){
		List<Map<String,String>> list=taskNodeService.getHistoryTasks(ShiroUtils.getUser());
		List<Map<String,String>> listPage= (List<Map<String, String>>) ListUtils.getSubList(list,tablepar.getPageNum(),tablepar.getPageSize());
		TableSplitResult<Map<String,String>> result=new TableSplitResult<Map<String,String>>(tablepar.getPageNum(),Long.valueOf(list.size()), listPage);
		return  result;
	}
	
	/**
     * 新增
     */

    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        return prefix + "/add";
    }
	
	//@Log(title = "任务节点新增", action = "111")
	@PostMapping("add")
	@ResponseBody
	public AjaxResult add(TaskNode taskNode){
		int b=taskNodeService.insertSelective(taskNode);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 删除
	 * @param ids
	 * @return
	 */
	//@Log(title = "任务节点删除", action = "111")
	@PostMapping("remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=taskNodeService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 检查
	 * @param
	 * @return
	 */
	@PostMapping("checkNameUnique")
	@ResponseBody
	public int checkNameUnique(TaskNode taskNode){
		int b=taskNodeService.checkNameUnique(taskNode);
		if(b>0){
			return 1;
		}else{
			return 0;
		}
	}
	
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("TaskNode", taskNodeService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }
	
	/**
     * 修改保存
     */
    //@Log(title = "任务节点修改", action = "111")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TaskNode record)
    {
        return toAjax(taskNodeService.updateByPrimaryKeySelective(record));
    }

    
    

	
}
